<?php
namespace app\admin\controller;

//品牌页
use app\admin\logic\BrandLogic;
use app\admin\logic\GoodsLogic;
use app\BaseController;
use think\facade\Request;
use think\facade\View;

class Brand extends BaseController {

    //brand  品牌列表
    public function brandList(){
        //where条件拼接
        list($where,$whereOr)=BrandLogic::brandWhere();
        //留值处理
        $data=BrandLogic::brandData();
        View::assign($data);
        //sql语句
        $brand=\app\admin\model\Brand::brandList($where,$whereOr,$data);
        $page=$brand->render();
        View::assign(['brand'=>$brand,'page'=>$page]);
        return View::fetch();
    }


    //brand  品牌添加
     public function brandAdd(){
            if (Request::isPost()){
               $post=Request::post();
                list($status, $paths) = Upload::imgUpload('brand_logo');
                if ($status && !empty($paths)) {
                    $post['brand_logo'] = $paths;
                }
            $res=\app\admin\model\Brand::brandAdd($post);
                if ($res){
                    $this->success('添加成功',url('brandList'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                return View::fetch();
            }


      }

     //brand 品牌修改
     public function brandEdit(){
        if (Request::isPost()){
            $param=Request::param();
            list($status, $brand_logo) = Upload::imgUpload('brand_logo');
            if (!$status){
                $this->error($brand_logo);
            }
            if ($status && !empty($brand_logo)) {
                list($path_date, $path_dir)=GoodsLogic::setImgUrl($brand_logo,'brand/');
                $param['brand_logo']=$path_dir;
            }
            $obj=\app\admin\model\Brand::find($param['brand_id']);
            $old= $obj->brand_logo;
            $res=\app\admin\model\Brand::brandEdit($param);
            if ($res){
                //单图移动临时目录到正式目录
                if ($status && !empty($brand_logo)) {
                    GoodsLogic::setImgDir($brand_logo, $path_date, $path_dir, './images/');
                }
                //删除单图老图路径
                if ($status && !empty($brand_logo) &&!empty($old)){unlink('./images/'.$old);}
                $this->success('修改成功',url('brandList'));
            }else{
                $this->error('修改失败');
            }
        }else{
            $param=Request::param();
            $find=\app\admin\model\Brand::brandEdit($param);
            View::assign($find);
            return View::fetch();

        }


      }

      //brand  品牌删除
     public function brandDel(){
        $get=Request::get();
      list($msg,$res)=\app\admin\model\Brand::brandDel($get);
        if ($res){
            $this->success($msg,url('brandList'));
        }elseif(!$res){
            $this->error($msg);
        }else{
            $this->error($msg);
         }

      }


}